package classes

import (
	"github.com/gin-gonic/gin"
	"github.com/jinzhu/gorm"
	"goft/src/goft"
	"goft/src/models"
	"log"
	"time"
)

type TestControl struct {
	*goft.GormAdapter
}

func NewTestControl() *TestControl {
	return &TestControl{}
}

func (this *TestControl) Build(g *goft.Goft) {
	g.Handle("GET", "/test/:id", this.Models)
}

func (this *TestControl) Models(ctx *gin.Context) goft.Model {
	test := models.NewTestModel()
	goft.Error(ctx.ShouldBindUri(test))
	db := this.Table("test").Where("id=?", test.ID).First(test)
	if db.Error != nil {
		panic(db.Error)
	}
	goft.Task(this.AddView, this.CallBack, test.ID)
	return test
}

//增加点击量
func (this *TestControl) AddView(parm ...interface{}) {
	time.Sleep(time.Second * 3)
	this.Table("test").Where("id=?", parm[0]).
		Update("userid", gorm.Expr("userid+1"))
}

//回调函数
func (this TestControl) CallBack() {
	log.Println("点击量执行完成")
}
